package com.qtjf.qtmoneyservice.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.utils.OperatorUtils;
import com.qtjf.qtmoneyservice.utils.RequestNetApi;

import java.util.HashMap;

import impl.IHttpListener;

public class PhoneRenZhengActivity extends BaseActivity {

    ImageView mBack;
    TextView phoneText;
    EditText passwordText;
    TextView sendBt;
    RadioButton radioButton;
    TextView nextBt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_ren_zheng);
        super.setTranslucent();

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mBack = (ImageView) findViewById(R.id.back_bt);
        phoneText= (TextView) findViewById(R.id.phone_text);
        passwordText= (EditText) findViewById(R.id.password_text);
        sendBt= (TextView) findViewById(R.id.send_bt);
        radioButton= (RadioButton) findViewById(R.id.radio_bt);
        nextBt= (TextView) findViewById(R.id.next_bt);

        phoneText.setText(Constant.getUsermobile()+"("+ OperatorUtils.execute(Constant.getUsermobile())+")");
    }

    private void initData() {
    }

    private void initEvent() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PhoneRenZhengActivity.this.finish();
            }
        });

        nextBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(radioButton.isChecked())
                {
                    if(phoneText.getText().toString().trim().length()==0||passwordText.getText().toString().trim().length()==0)
                    {
                        Toast.makeText(PhoneRenZhengActivity.this,"手机号或密码未填写",Toast.LENGTH_SHORT).show();
                    }else
                    {
                        renZhengRequest();
                    }
                }else
                {
                    Toast.makeText(PhoneRenZhengActivity.this,"请同意相应条款",Toast.LENGTH_SHORT).show();
                }
            }
        });

        sendBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(phoneText.getText().toString().trim().length()==11) {
                    Intent intent = new Intent(PhoneRenZhengActivity.this, ForgetPasswordActivity.class);
                    intent.putExtra("phoneNumber", phoneText.getText().toString().trim());
                    startActivity(intent);
                }else
                {
                    Toast.makeText(PhoneRenZhengActivity.this,"请输入有效的手机号",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void renZhengRequest()
    {
        //http://118.89.185.176:8082/jdt/submitMobile
        RequestNetApi requestNetApi=new RequestNetApi();
        requestNetApi.attach(new IHttpListener() {
            @Override
            public void success(String msg) {
                Log.d("999",msg);
            }

            @Override
            public void fail() {

                Toast.makeText(PhoneRenZhengActivity.this,"网络请求失败",Toast.LENGTH_SHORT).show();
            }
        });

        //http://118.89.185.176:8082/tanzhi/sumbitBank

        HashMap<String,String> map=new HashMap<String, String>();
        map.put("token",Constant.getTOKEN());
        map.put("dk",Constant.APPKEY);
        map.put("password",passwordText.getText().toString());
        map.put("userId", Constant.getUserId());
        Log.d("tab",Constant.getTOKEN()+"   "+Constant.APPKEY+"   "+passwordText.getText().toString()+"   "+Constant.getUserId());

        requestNetApi.postHttp("jdt/submitMobile",map);
    }
}
